package com.fanli.scheduler.serviceImpl;

import com.fanli.scheduler.bean.HiveEsTemplate;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.Client;
import com.fanli.scheduler.service.ESService;
import com.fanli.scheduler.utils.ElasticSearchManager;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by xu.liu on 2016/9/7.
 */

@Service
public class ESServiceImpl implements ESService{

    public List<HiveEsTemplate> convertToEsType(List<HiveEsTemplate> resource){
        for (HiveEsTemplate entry:resource) {
            String type="";
            if (entry.getColumnType().toLowerCase().startsWith("array")){
                type="string";
            }else if (entry.getColumnType().toLowerCase().startsWith("bigint")){
                type="long";
            }else if (entry.getColumnType().toLowerCase().startsWith("boolean")){
                type="string";
            }else if (entry.getColumnType().toLowerCase().startsWith("date")){
                type="date";
            }else if (entry.getColumnType().toLowerCase().startsWith("decimal")){
                type="double";
            }else if (entry.getColumnType().toLowerCase().startsWith("double")){
                type="double";
            }else if (entry.getColumnType().toLowerCase().startsWith("float")){
                type="double";
            }else if (entry.getColumnType().toLowerCase().startsWith("int")){
                type="integer";
            }else if (entry.getColumnType().toLowerCase().startsWith("map")){
                type="string";
            }else if (entry.getColumnType().toLowerCase().startsWith("smallint")){
                type="integer";
            }else if (entry.getColumnType().toLowerCase().startsWith("string")){
                type="string";
            }else if (entry.getColumnType().toLowerCase().startsWith("timestamp")){
                type="string";
            }else if (entry.getColumnType().toLowerCase().startsWith("tinyint")) {
                type="integer";
            }else if (entry.getColumnType().toLowerCase().startsWith("varchar")){
                type="string";
//            }else if (entry.getColumnType().toLowerCase().startsWith("void")){
//                type="string";
            } else{
                throw  new RuntimeException();
            }
            entry.setColumnType(type);
        }
        return resource;
    }

    @Override
    public boolean createEsTemplate(String templateName, String templateIndex,String templateMapName, List<HiveEsTemplate> colunms) throws IOException {
        colunms=convertToEsType(colunms);

        XContentBuilder builder =XContentFactory.jsonBuilder()
                .startObject()
                .startObject("properties");
        for (HiveEsTemplate colunm:colunms) {
            if(colunm.getIsInclude()){
                builder.startObject(colunm.getColumnName()).field("type", colunm.getColumnType());
                if (colunm.isIndex()){
                    builder.field("index", "analyzed");
                }else{
                    builder.field("index", "not_analyzed");
                }
                builder.endObject();
            }
        }
        builder.endObject().endObject();

        AdminClient adminClient = ElasticSearchManager.getEsClient().admin();
        PutIndexTemplateResponse response=adminClient.indices()
                .preparePutTemplate(templateName)  //my_template模板名称
                .setTemplate(templateIndex)   //可以被任何以product开头的索引使用
                .setSettings(Settings.builder())
//                        .put("index.number_of_replicas", 2)  //2个副本
//                        .put("index.number_of_shards", 1))   //一个分片
                .addMapping(templateMapName, builder)
                .execute().actionGet();
//        adminClient.indices().preparePutTemplate("").execute().actionGet();
        return response.isAcknowledged();
    }

    @Override
    public boolean deleteEsTemplate(String templateNmae, String templateInedx) {
        return false;
    }
}
